<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * 文件遍历类的PHP5以上版本实现
 +------------------------------------------------------------------------------
 * @category   ORG
 * @package  ORG
 * @subpackage  Io
 * @author    liu21st <liu21st@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */
class FileIterator  extends Base implements Iterator
{//类定义开始

    /**
     +----------------------------------------------------------
     * 文件名
     +----------------------------------------------------------
     * @var string
     * @access private
     +----------------------------------------------------------
     */
    var $_filename  ;

    /**
     +----------------------------------------------------------
     * 缓存读取大小
     +----------------------------------------------------------
     * @var integer
     * @access private
     +----------------------------------------------------------
     */
    var $_buffer    ;

    /**
     +----------------------------------------------------------
     * 文件句柄
     +----------------------------------------------------------
     * @var resource
     * @access private
     +----------------------------------------------------------
     */
    var $_fp        ;

    /**
     +----------------------------------------------------------
     * 当前行
     +----------------------------------------------------------
     * @var integer
     * @access private
     +----------------------------------------------------------
     */
    var $_current   ;

    /**
     +----------------------------------------------------------
     * 当前行数
     +----------------------------------------------------------
     * @var integer
     * @access private
     +----------------------------------------------------------
     */
    var $_key       ;

    /**
     +----------------------------------------------------------
     * 有效性
     +----------------------------------------------------------
     * @var boolen
     * @access private
     +----------------------------------------------------------
     */
    var $_valid     ;


    /**
     +----------------------------------------------------------
     * 架构函数
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $elements  初始化数组元素
     +----------------------------------------------------------
     */
    function __construct($filename, $buffer = 1024)
    {
        $this->_filename = $filename;
        $this->_buffer   = $buffer;
        $this->_key      = -1;

        if (file_exists($filename) && is_readable($filename)) {
            $this->_fp = fopen($filename, 'rb');
        }
    }


    /**
     +----------------------------------------------------------
     * 取得文件当前行
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return mixed
     +----------------------------------------------------------
     */
    function current()
    {
        return $this->_current;
    }


    /**
     +----------------------------------------------------------
     * 当前行数
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return mixed
     +----------------------------------------------------------
     */
    function key()
    {
        return $this->_key;
    }


    /**
     +----------------------------------------------------------
     * 指针移动到下一个
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    function next()
    {
        if (is_resource($this->fp)) {
            if (!feof($this->fp)) {
                $this->_key++;
                $this->_current = fgets($this->_fp, $this->_buffer);
                $this->_valid   = true;

            } else {
                $this->_valid = false;
            }
        }
    }


    /**
     +----------------------------------------------------------
     * 重置指针位置 回到开始位置
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    function rewind()
    {
        if (is_resource($this->_fp)) {
            $this->_key = -1;
            fseek($this->_fp, 0);
            $this->next();
        }
    }


    /**
     +----------------------------------------------------------
     * 指针位置是否有效
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    function valid()
    {
        return $this->_valid;
    }


    /**
     +----------------------------------------------------------
     * 析构函数 关闭文件
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    function __destruct( ) {
        fclose($this->_fp);
    }

}//类定义结束
?>